// pages/goods/goodsList/goodsList.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        parentClassId: null,
        classId: null,
        classInfo: {},
        classImg: null,
        classList: [],
        goodsList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false,
        imgHeight: 0,
        navFixed: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
      this.setData({
          parentClassId: options.classId,
          classId: options.classId
      })
      this.getClassList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
       
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 1000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    lower: function() {
        this.getGoodsList()
    },

    scroll: function(e) {
        if (this.data.classImg) {
            if (e.detail.scrollTop > this.data.imgHeight) {
                this.setData({
                    navFixed: true
                })
            } else {
                this.setData({
                    navFixed: false
                })
            }
        }
    },

    getClassList: function() {
        const that = this
        let data = {
            activity: 785,
            class_id: this.data.parentClassId
        }
        common.post('Goodsclass/get_class', data).then(function(res) {
            that.setData({
                classInfo: res,
                classList: res.list,
                classImg: res.class_img
            })
            if (that.data.classImg) {
                that.calHeight()
            } else {
                that.setData({
                    navFixed: true
                })
            }
            that.getGoodsList()
        }).catch(function(e) {})
    },

    getGoodsList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let goodsList = this.data.goodsList
        let data = {
            is_goods:99,
            activity: 785,
            class_id: this.data.classId,
            limit: 10,
            page: this.data.nowPage
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    goodsList: goodsList,
                    isAll: true
                })
            } else {
                goodsList = goodsList.concat(res)
                that.setData({
                    goodsList: goodsList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    navClick: function(e) {
        if (e.detail.index == -1) {
            this.setData({
                classImg: this.data.classInfo.class_img
            })
        } else {
            this.setData({
                classImg: this.data.classList[e.detail.index].class_img
            })
        }
        this.setData({
            classId: e.detail.id,
            goodsList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false,
            imgHeight: 0,
            navFixed: false
        })
        this.getGoodsList()
    },

    calHeight: function() {
        const that = this
        let query = wx.createSelectorQuery()
        query.select('#fixImg').boundingClientRect()
        query.exec(function(res) {
            //取高度
            that.setData({
                imgHeight: res[0].height
            })
        })
    }
})
